package com.tubeonfire.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tubeonfire.entity.Category;
import com.tubeonfire.entity.Channel;
import com.tubeonfire.entity.Playlist;
import com.tubeonfire.entity.Tube;
import com.tubeonfire.model.CategoryModel;
import com.tubeonfire.model.PlaylistModel;
import com.tubeonfire.service.ChannelService;
import com.tubeonfire.service.TubeService;
import com.tubeonfire.util.JavaCacheHandle;

public class CacheController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");		
		JavaCacheHandle.initCache();
		JavaCacheHandle.removeCache();
		List<Channel> listChannel = ChannelService.getMostViewdToday();
		if (listChannel != null && listChannel.size() > 0) {
			JavaCacheHandle.addChannels(listChannel);
		}

		List<Tube> listMostViewToday = TubeService.getMostView("today", 6, 1)
				.getListTube();
		if (listMostViewToday != null && listMostViewToday.size() > 0) {
			JavaCacheHandle.addMostViewToday(listMostViewToday);
		}

		List<Tube> listMostViewAllTime = TubeService.getMostView("all_time", 6,
				1).getListTube();
		if (listMostViewAllTime != null && listMostViewAllTime.size() > 0) {
			JavaCacheHandle.addMostViewAllTime(listMostViewToday);
		}

		List<Tube> listPopular = TubeService.getMostPopular("all_time");
		if (listPopular != null && listPopular.size() > 0) {
			JavaCacheHandle.addPopular(listPopular);
		}

		List<Tube> listRecent = TubeService.getMostRecent();
		if (listRecent != null && listRecent.size() > 0) {
			JavaCacheHandle.addRecent(listRecent);
		}

		List<Category> listCategory = CategoryModel.all();
		if (listCategory != null && listCategory.size() > 0) {
			JavaCacheHandle.addCategory(listCategory);
		}

		List<Playlist> listPl = PlaylistModel.all();
		if (listPl != null && listPl.size() > 0) {
			JavaCacheHandle.addPl(listPl);
		}
		response.getWriter().print("Ok");
	}

}
